            alter table ganado add Tatuaje varchar(10)
            GO
            delete from parametros where item in (1,3,4,5,7,9,10,15,16,17,18)
            GO
            CREATE NONCLUSTERED INDEX [IX_OPERACIONES_FECHA] ON [dbo].[OPERACIONES] 
             (
              [FECHA] ASC,
              [OPERACION] ASC
             )
            GO
             SET IDENTITY_INSERT PARAMETROS  ON
            insert into parametros 
            (item,[DESCRIPCION],[VALOR]) values (19,'Peso minimo Aceptable','40')
            insert into parametros 
            (item,[DESCRIPCION],[VALOR]) values (20,'Peso Maximo Aceptable','600')
            insert into parametros 
            (item,[DESCRIPCION],[VALOR]) values (21,'Peso Kilo Minimo Aceptable','1800')
            insert into parametros 
            (item,[DESCRIPCION],[VALOR]) values (22,'Peso Kilo Maximo Aceptable','3300')
            insert into parametros 
            (item,[DESCRIPCION],[VALOR]) values (23,'Peso Al ojo minimo Aceptable','200000')
            insert into parametros 
            (item,[DESCRIPCION],[VALOR]) values (24,'Peso Al ojo maximo Aceptable','800000')
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[RepGananciaVida]'))
            DROP VIEW [dbo].[RepGananciaVida]
            GO
            CREATE VIEW [dbo].[RepGananciaVida] AS
            SELECT O1.Codigo AS Codigo, 
            O1.FECHA AS FechaInicial, 
            O1.Peso AS PesoInicial,
            O2.FECHA AS FechaFinal, 
            O2.Peso AS PesoFinal,
            CASE O1.PESO 
            WHEN 0 THEN NULL
            ELSE
            ROUND((cast((O2.PESO-O1.PESO) as float)/datediff(dd,O1.FECHA,O2.FECHA))*1000,0) END  AS GananciaDia 
            FROM PESAJES AS O1 INNER JOIN PESAJES AS O2 ON O1.Codigo = O2.Codigo 
            INNER JOIN GANADO G ON G.Codigo = O2.Codigo  
            WHERE [O1].[FECHA] < [O2].[FECHA] AND
            [O1].[FECHA]  = (SELECT MIN(FECHA) FROM PESAJES P WHERE P.CODIGO = O1.CODIGO) AND
            [O2].[FECHA]  = (SELECT MAX(FECHA) FROM PESAJES P WHERE P.CODIGO = O2.CODIGO)
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Ventas]'))
            DROP VIEW [dbo].[Ventas]
            GO
            CREATE VIEW [dbo].[Ventas]
            AS
            SELECT 
                  Fecha, 
                  Codigo, 
                  VALOR1 as Kilos , 
                  VALOR2 as PrecioKilo,
                  VALOR1*VALOR2 as Precio
            FROM OPERACIONES 
            WHERE OPERACION = 7
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Compras]'))
            DROP VIEW [dbo].[Compras]
            GO
            CREATE VIEW [dbo].[Compras]
            AS
            SELECT 
                  Fecha, 
                  Codigo, 
                  VALOR1 as Kilos , 
                  VALOR2 as PrecioKilo,
                  CASE WHEN VALOR1 = 0 THEN  VALOR2 
                  ELSE VALOR1*VALOR2 END AS Precio
            FROM OPERACIONES 
            WHERE OPERACION = 6  
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[VentasVsCompras]'))
            DROP VIEW [dbo].[VentasVsCompras]
            GO
            CREATE VIEW [dbo].[VentasVsCompras] AS
            SELECT A.FECHA AS Fecha_Venta, A.Codigo, 
            A.kilos AS Peso_Venta,
            A.PrecioKilo AS Precio_Kilo,
            A.Precio AS Valor_Venta,
            B.PrecioKilo AS Costo_Kilo,
            B.FECHA AS Fecha_Compra,
            B.Precio As Costo_Compra,
            B.Kilos AS Kilos_Compra,
            datediff(dd,B.FECHA ,A.FECHA ) as NumeroDias,
            CASE B.kilos WHEN 0 THEN NULL
            ELSE
            round(((A.kilos - B.kilos) / datediff(dd,B.Fecha,A.Fecha))* 1000,0) END
             as GananciaDiariaGramos ,
            round(((A.Precio - B.Precio) / datediff(dd,B.Fecha,A.Fecha)),0)
             as GananciaDiariaPesos ,
            CASE B.kilos WHEN 0 THEN B.PrecioKilo ELSE B.Precio END AS Costo
            FROM VENTAS A LEFT OUTER JOIN COMPRAS B ON B.CODIGO=A.CODIGO 
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[RepMovimientos]'))
            DROP VIEW [dbo].[RepMovimientos]
            GO
            CREATE VIEW [dbo].[RepMovimientos] AS
            SELECT FECHA,
            CASE   OPERACION WHEN 6 THEN 'COMPRA' 
            WHEN 7 THEN 'VENTA' 
            WHEN 9 THEN 'MUERTE' END AS OPERACION 
            ,COUNT(*) *  
            (CASE OPERACION WHEN 6 THEN 1 
            WHEN 7 THEN -1 
            WHEN 9 THEN -1  END)
            AS TOTAL_INV,
            COUNT(*) AS TOTAL  
            FROM  dbo.OPERACIONES
            WHERE OPERACION IN (6,7,9)
            GROUP BY FECHA,OPERACION
            GO
            delete from pesajes where id in
            (select MaxId from (
            (select [Fecha],[Codigo],count(*) c1,max(id) as MaxID 
            from pesajes
            group by [Fecha],[Codigo]
            having count(*)>1)) as T)
            GO
            IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[PESAJES]') AND name = N'IX_PESAJES_FECHA')
            DROP INDEX [IX_PESAJES_FECHA] ON [dbo].[PESAJES] 
            GO
            IF  EXISTS (SELECT * FROM sys.indexes WHERE 
                        object_id = OBJECT_ID(N'[dbo].[PESAJES]') AND name = N'IX_PESAJES_UNIQUE')
            DROP INDEX [IX_PESAJES_UNIQUE] ON [dbo].[PESAJES] 
            GO
            CREATE UNIQUE INDEX  [IX_PESAJES_FECHA] ON [dbo].[PESAJES] 
            ([Fecha] ASC,[Codigo] ASC
            )
            GO
            DROP TABLE POTREROS
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[PrimerPesajeCarne]'))
            DROP VIEW [dbo].[PrimerPesajeCarne]
            GO
            CREATE VIEW [dbo].[PrimerPesajeCarne]
            AS 
            SELECT CODIGO, PESO, FECHA 
            FROM PESAJES  AS O1
            WHERE FECHA = (
                     SELECT MIN(FECHA)
                     FROM PESAJES  AS O2
                     WHERE O2.CODIGO = O1.CODIGO)
             GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[ultimopesajecarne]'))
            DROP VIEW [dbo].[ultimopesajecarne]
            GO
            CREATE VIEW [dbo].[ultimopesajecarne]
            AS
            SELECT DISTINCT CODIGO, PESO , fecha
            FROM PESAJES  AS O1
            WHERE FECHA = ( SELECT MAX(FECHA)
                            FROM PESAJES  AS O2
                            WHERE O2.CODIGO = O1.CODIGO)
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[InventarioDetallado]'))
            DROP VIEW [dbo].[InventarioDetallado]
            GO
            CREATE VIEW [dbo].[InventarioDetallado] AS
            SELECT dbo.GANADO.CODIGO,
             CASE dbo.COLORES.ID WHEN 0 THEN '-' ELSE dbo.COLORES.DESCRIPCION END AS Color, 
             CASE dbo.RAZAS.ID WHEN 0 THEN '-' ELSE dbo.RAZAS.DESCRIPCION END AS Raza, dbo.ultimopesajecarne.PESO AS UltimoPeso, CONVERT(varchar, 
             dbo.ultimopesajecarne.fecha, 101) AS Fecha, dbo.GANADO.Tatuaje
            FROM  dbo.GANADO INNER JOIN
               dbo.COLORES ON dbo.GANADO.COLOR = dbo.COLORES.ID INNER JOIN
               dbo.RAZAS ON dbo.GANADO.RAZA1 = dbo.RAZAS.ID AND dbo.GANADO.RAZA2 = dbo.RAZAS.ID INNER JOIN
               dbo.ultimopesajecarne ON dbo.GANADO.CODIGO = dbo.ultimopesajecarne.CODIGO
            WHERE (dbo.GANADO.Activo = 1)
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[Muertes]'))
            DROP VIEW [dbo].[Muertes]
            GO
            CREATE VIEW [dbo].[Muertes]
            AS
            SELECT TOP 100 percent
                  Fecha, 
                  Codigo,
                  DESCRIPCION AS Causa
            FROM OPERACIONES LEFT OUTER JOIN COMENTARIOS
            ON OPERACIONES.NCOMENTARIO = COMENTARIOS.NCOMENTARIO 
            AND OPERACIONES.OPERACION = COMENTARIOS.OPERACION
            WHERE OPERACIONES.OPERACION = 9  
            ORDER BY FECHA DESC
            GO
            IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[RepPesajesYGanancia]'))
            DROP VIEW [dbo].[RepPesajesYGanancia]
            GO
            CREATE VIEW [dbo].[RepPesajesYGanancia] AS
            SELECT O1.Codigo AS CODIGO, 
            O1.FECHA AS Fecha, 
            O1.Peso AS Peso,
            ISNULL(ROUND((cast((O2.PESO-O1.PESO) as float)/datediff(dd,O1.FECHA,O2.FECHA))*1000,0),0)  AS Promedio 
            FROM PESAJES AS O1 LEFT OUTER JOIN PESAJES AS O2 ON O1.Codigo = O2.Codigo AND
            [O2].[FECHA] < [O1].[FECHA]
            and 
            [O2].[FECHA]  = (SELECT MAX(FECHA) 
                             FROM PESAJES P3 WHERE P3.CODIGO = O2.CODIGO 
                             and [P3].[FECHA] < [O1].[FECHA]
            ) 
            GO
            SET IDENTITY_INSERT PARAMETROS  ON
            if not exists(select * from parametros where item = 25)
            begin
                insert into parametros (item,descripcion,valor)
                values (25,'Fecha Ultimo Backup','1900-01-01');
            end
            GO
            if not exists(select * from lotes where ID = 0)
            begin
                insert into lotes(ID,descripcion)
                values (0,'-');
            end
            GO
	    ALTER VIEW [dbo].[InventarioDetallado] AS
  	    SELECT dbo.GANADO.CODIGO,
            CASE dbo.COLORES.ID WHEN 0 THEN '-' ELSE dbo.COLORES.DESCRIPCION END AS Color, 
            CASE dbo.RAZAS.ID WHEN 0 THEN '-' ELSE dbo.RAZAS.DESCRIPCION END AS Raza, dbo.ultimopesajecarne.PESO AS UltimoPeso, CONVERT(varchar, 
            dbo.ultimopesajecarne.fecha, 101) AS Fecha, dbo.GANADO.Tatuaje
	    FROM  dbo.GANADO LEFT OUTER JOIN
            dbo.COLORES ON dbo.GANADO.COLOR = dbo.COLORES.ID LEFT OUTER JOIN
            dbo.RAZAS ON dbo.GANADO.RAZA1 = dbo.RAZAS.ID LEFT OUTER JOIN
            dbo.ultimopesajecarne ON dbo.GANADO.CODIGO = dbo.ultimopesajecarne.CODIGO
	    WHERE (dbo.GANADO.Activo = 1)
	    GO


